package com.synv.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.sax.StartElementListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.synv.entity.Category;
import com.synv.flashcard.CategoryDetailActivity;
import com.synv.flashcard.ContentActivity;
import com.synv.flashcard.R;
import com.synv.flashcard.WordDetailActivity;

public class CategoryAdapter extends ArrayAdapter<Category>{

	Context context; 
    int layoutResourceId;
    Category[] categories;
    
    public CategoryAdapter(Context context, int layoutResourceId, Category[] categories) {
		super(context, layoutResourceId, categories);

		this.context = context;
		this.layoutResourceId = layoutResourceId;
		this.categories = categories;
	}
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;
        CategoryHolder holder = null;
        
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            
            holder = new CategoryHolder();
            //holder.cardBookmark = (CheckBox)row.findViewById(R.id.cardBookmark);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
            holder.btnCategoryEdit =(Button) row.findViewById(R.id.btnCategoryEdit);
            
            row.setTag(holder);
        }
        else
        {
            holder = (CategoryHolder)row.getTag();
        }
        
        final Category category = categories[position];
        holder.txtTitle.setText(category.getName());
        /*holder.txtTitle.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent contentScreen = new Intent(context, ContentActivity.class);
				contentScreen.putExtra("categorySeq", category.getSeq());
				context.startActivity(contentScreen);
			}
		});*/
        
        /*holder.cardBookmark.setChecked(category.getBookmark() > 0 ? true : false);
        holder.cardBookmark.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				CheckBox checkbox = (CheckBox) arg0;
				Category category = categories[position];
				bookmark(category);
			}
        	
        });*/
        
        holder.btnCategoryEdit.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Category category = categories[position];
				onclick(category);
			}
		});
        return row;
    }
    
    private void onclick(Category category) {
    	
		Intent categoryDetail = new Intent(context, CategoryDetailActivity.class);
		categoryDetail.putExtra("categorySeq", category.getSeq());
		categoryDetail.putExtra("categoryName", category.getName());
		context.startActivity(categoryDetail);
    }
    
    private void bookmark(Category category) {
    	if(category.getBookmark() != Category.ALL_MARK) {
    		category.setBookmark(Category.ALL_MARK);
    	} else if (category.getBookmark() == Category.ALL_MARK) {
    		category.setBookmark(Category.NOT_MARK);
    	}
    	try {
	    	DataBaseHelper myDbHelper = new DataBaseHelper(context);
	    	myDbHelper.getWritableDatabase().execSQL("UPDATE flashcard SET Bookmark = " + category.getBookmark() +" WHERE Category LIKE '%," + category.getSeq() + ",%'");
	    	myDbHelper.getWritableDatabase().execSQL("UPDATE category SET IsBookmark = " + category.getBookmark() + " WHERE Seq = " + category.getSeq());
	    	myDbHelper.close();
	    	Toast.makeText(context, "Bookmark successfully!", Toast.LENGTH_SHORT).show();
    	} catch (Exception ex) {
    		Toast.makeText(context, "Bookmark failure!", Toast.LENGTH_SHORT).show();
    	}
    }
    
    @Override
    public Category getItem(int position) {
    	return categories[position];
    }
    
    static class CategoryHolder
    {
        ImageView imgCard;
        TextView txtTitle;
        //CheckBox cardBookmark;
        Button btnCategoryEdit;
    }
}
